package alo.p3;

public class function3 {


    //统计单向循环链表的结点个数
    int CircularListLength(CLLNode headNode){
        int length=0;
        CLLNode currentNode=headNode;
        while (currentNode!=null){
            length++;
            currentNode=currentNode.getNext();
            if (currentNode==headNode)
                break;
        }
        return length;
    }



    //输出循环链表的内容
    void PrintCircularListData(CLLNode headNode){
        CLLNode CLLNode=headNode;
        while (CLLNode!=null){
            System.out.println(CLLNode.getData()+"->");
            CLLNode=CLLNode.getNext();
            if (CLLNode== headNode) break;
        }
        System.out.println("("+CLLNode.getData()+")headNode");

    }


    //在循环链表的表尾插入结点
    void InsertAtEndInCLL(CLLNode headNode,CLLNode nodeToInsert){
        CLLNode currentNOde=headNode;
        while (currentNOde.getNext()!=headNode){
            currentNOde.setNext(currentNOde.getNext());
        }
        //初始化，如果headNode为空，直接赋值给headNode
        nodeToInsert.setNext(nodeToInsert);
        if (headNode==null){
            headNode=nodeToInsert;
        }else {
            nodeToInsert.setNext(headNode);
            currentNOde.setNext(nodeToInsert);
        }
    }

    //在循环链表的表头插入结点
    void InsertAtBeginInCLL(CLLNode headNode,CLLNode nodeToInsert){
        CLLNode currentNOde=headNode;
        while (currentNOde.getNext()!=headNode){
            currentNOde.setNext(currentNOde.getNext());
        }
        //初始化，如果headNode为空，直接赋值给headNode
        nodeToInsert.setNext(nodeToInsert);
        if (headNode==null){
            headNode=nodeToInsert;
        }else {
            nodeToInsert.setNext(headNode);
            currentNOde.setNext(nodeToInsert);
            headNode=nodeToInsert;
        }
    }

    //删除循环链表中的最后一个结点
    void DeleteLastNodeFromCLL(CLLNode headNode){
        CLLNode temp=headNode;
        CLLNode currentNode=headNode;
        if (headNode==null){
            System.out.println("List Empty!");
            return;
        }
        while (currentNode.getNext()!=headNode){
            temp=currentNode;
            currentNode=currentNode.getNext();
        }
        temp.setNext(headNode);
        currentNode=null;
        return;
    }


    //删除循环链表中的第一个结点
    void DeleteFrontNodeFromCLL(CLLNode headNode){
        CLLNode temp=headNode;
        CLLNode currentNode=headNode;
        if (headNode==null){
            System.out.println("List Empty!");
            return;
        }
        while (currentNode.getNext()!=headNode){
            currentNode.setNext(currentNode.getNext());
        }
        currentNode.setNext(headNode.getNext());
        headNode=headNode.getNext();
        temp=null;
        return;
    }

}
